package ex_from_web;

class MyThread implements Runnable{
    private String name;
    
    public MyThread(String n) {
        name = n;
    }

    public void run() {
        System.out.println(name + " started");
        for (int i=0; i<5; i++) {
            try {
                System.out.println(name + " - " + i);
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                System.out.println("Thread pause interrupted!");
            }
        }
        System.out.println(name + " finished");
    }
    
}

public class join_a_thread {
    public static void main(String[] args) {
        Thread t1 = new Thread(new MyThread("1st"));
        Thread t2 = new Thread(new MyThread("2nd"));
        Thread t3 = new Thread(new MyThread("3rd"));
        
        t1.start();
        t2.start();
        //Wait for the thread t2 to die (to finish its job)
        try {
            t2.join();
        } catch (InterruptedException e) {
            System.out.println("Join thread interrupted!");
        }
        //After the thread t2 dies, start the thread t3
        t3.start();
    }

}